﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TD.Interface.Controls;
using TD.TheGame;

namespace TD.Interface
{
    internal class HUD : MenuBase
    {
        private Texture2D texture;
        private PlayerComponent player;
        private LevelComponent level;
        private Castle castle;

        private int maxHealth;
        private int curHealth;
        private int points;
        private int wave;

        // Health (Total/per castle)
        // Wave (x/y)
        // Enemies Remaining (x)
        // Toggle Mute (True/False)

        public HUD(Game1 game) :
            base(game)
        {
            this.DrawOrder = 0x08000000;
            this.Enabled = true;
            this.Visible = true;
        }

        public override void Initialize()
        {
            player = (PlayerComponent)Game.Services.GetService(typeof(PlayerComponent));
            level = (LevelComponent)Game.Services.GetService(typeof(LevelComponent));
            castle = (Castle)Game.Services.GetService(typeof(Castle));

            base.Initialize();
        }

        protected override void  LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Fonts
            font = base.Game.Content.Load<SpriteFont>("interface/MenuFont");
            subFont = base.Game.Content.Load<SpriteFont>("interface/MenuSubFont");

            // Textures
            texture = ((Game1)Game).TextureManager.UseAsset("white");
            
            base.LoadContent();
        }

        protected override List<DrawableGameComponent> AddItems()
        {
            return new List<DrawableGameComponent>();
        }

        protected override void UpdateItems(List<DrawableGameComponent> items, GameTime gameTime)
        {
            //throw new NotImplementedException();
        }

        public override void Update(GameTime gameTime)
        {
            curHealth = castle.Health;
            maxHealth = castle.MaxHealth;
            points = level.Points;
            wave = (level.currLevel + 1);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.Opaque);
            spriteBatch.Draw(texture, new Rectangle(40, 72, ((488 * curHealth) / maxHealth), 10), Color.Lime);
            spriteBatch.Draw(texture, new Rectangle(40, 72, 488, 10), Color.Red);
            spriteBatch.End();

            Color color = Color.Black;

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.DrawString(font, (curHealth.ToString() + " \\ " + maxHealth.ToString()), new Vector2(40, 33), color);
            spriteBatch.DrawString(font, ("Wave: " + wave.ToString()), new Vector2(752, 33), color);
            spriteBatch.DrawString(font, ("Points: " + points), new Vector2(960, 33), color);
            spriteBatch.End();
            
            base.Draw(gameTime);
        }

        protected override void HandleEnabledChanged(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        protected override void HandleVisibleChanged(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }
    }
}
